package net.xujialiang.XTestRPA.Controller.SuperAdmin;

import net.xujialiang.XTestRPA.Config.SpringSecurityConfig.SecurityUser;
import net.xujialiang.XTestRPA.Entity.Tenant.XRPATenant;
import net.xujialiang.XTestRPA.Model.ResultData;
import net.xujialiang.XTestRPA.Service.XRPASuperAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

import static net.xujialiang.XTestRPA.Enum.ReturnCode.RC999;

@RequestMapping("/api/admin")
@RestController
public class SuperAdmin {

    @Autowired
    XRPASuperAdminService superAdminService;

    @RequestMapping(value = "/fetchTenantInfo", method = RequestMethod.POST)
    @ResponseBody
    public ResultData fetchTenantInfo(@RequestBody Map<String, Object> reqBody, HttpServletRequest request, Authentication auth) {
        Integer page = (Integer) reqBody.get("page");
        Integer pageSize = (Integer) reqBody.get("pageSize");
        SecurityUser sUser = (SecurityUser) auth.getPrincipal();
        String createAccount = sUser.getUsername();
        String ttc = sUser.getTTC();
        Integer id = (Integer) reqBody.getOrDefault("id", -1);
        if(id>0){
            try {
                if(createAccount.equals("admin") && ttc.equals("admin")) {
                    XRPATenant ret = superAdminService.fetchSingle(id);
                    return ResultData.success(ret);
                }else{
                    return ResultData.fail(RC999.getCode(), RC999.getMessage());
                }
            }catch (Exception e){
                return ResultData.fail(RC999.getCode(), e.getMessage());
            }
        }else{
            try {
                if(createAccount.equals("admin") && ttc.equals("admin")) {
                    Map<String, Object> ret = superAdminService.fetch(page, pageSize);
                    return ResultData.success(ret);
                }else{
                    return ResultData.fail(RC999.getCode(), RC999.getMessage());
                }
            }catch (Exception e){
                return ResultData.fail(RC999.getCode(), e.getMessage());
            }
        }
    }

    @RequestMapping(value = "/searchTenantInfo", method = RequestMethod.POST)
    @ResponseBody
    public ResultData searchTenantInfo(@RequestBody Map<String, Object> reqBody, HttpServletRequest request, Authentication auth) {
        SecurityUser sUser = (SecurityUser) auth.getPrincipal();
        String createAccount = sUser.getUsername();
        String ttc = sUser.getTTC();
        String searchTTC = (String) reqBody.get("ttc");
        String admin = (String) reqBody.get("admin");
        Integer page = (Integer) reqBody.getOrDefault("page", 0);
        Integer pageSize = (Integer) reqBody.getOrDefault("pageSize", 10);
        try {
            if(createAccount.equals("admin") && ttc.equals("admin")) {
                Map<String, Object> ret = superAdminService.search(searchTTC, admin, page, pageSize);
                return ResultData.success(ret);
            }else{
                return ResultData.fail(RC999.getCode(), RC999.getMessage());
            }
        }catch (Exception e){
            return ResultData.fail(RC999.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/updateTenantInfo", method = RequestMethod.POST)
    @ResponseBody
    public ResultData updateTenantInfo(@RequestBody Map<String, Object> reqBody, HttpServletRequest request, Authentication auth) throws Exception {
        SecurityUser sUser = (SecurityUser) auth.getPrincipal();
        String createAccount = sUser.getUsername();
        String ttc = sUser.getTTC();

        try {
            if(createAccount.equals("admin") && ttc.equals("admin")) {
                superAdminService.updateTenantInfo(reqBody);
                return ResultData.success();
            }else{
                return ResultData.fail(RC999.getCode(), RC999.getMessage());
            }
        }catch (Exception e){
            return ResultData.fail(RC999.getCode(), RC999.getMessage());
        }

    }

    @RequestMapping(value = "/delTenant", method = RequestMethod.POST)
    @ResponseBody
    public ResultData delTenant(@RequestBody Map<String, Object> reqBody, HttpServletRequest request, Authentication auth) throws Exception {
        SecurityUser sUser = (SecurityUser) auth.getPrincipal();
        String createAccount = sUser.getUsername();
        String ttc = sUser.getTTC();
        Integer id = (Integer) reqBody.getOrDefault("id", -1);

        try {
            if(createAccount.equals("admin") && ttc.equals("admin")) {
                superAdminService.delTenant(id);
                return ResultData.success();
            }else{
                return ResultData.fail(RC999.getCode(), RC999.getMessage());
            }
        }catch (Exception e){
            return ResultData.fail(RC999.getCode(), RC999.getMessage());
        }

    }

    @RequestMapping(value = "/addRPAPoints", method = RequestMethod.POST)
    @ResponseBody
    public ResultData addRPAPoints(@RequestBody Map<String, Object> reqBody, HttpServletRequest request, Authentication auth) throws Exception {
        SecurityUser sUser = (SecurityUser) auth.getPrincipal();
        String createAccount = sUser.getUsername();
        String ttc = sUser.getTTC();
        Integer points = (Integer) reqBody.getOrDefault("points", 0);
        String opTTC = (String) reqBody.get("ttc");
        try {
            if(createAccount.equals("admin") && ttc.equals("admin")) {
                superAdminService.addRpaPoints(opTTC, points);
                return ResultData.success();
            }else{
                return ResultData.fail(RC999.getCode(), RC999.getMessage());
            }
        }catch (Exception e){
            return ResultData.fail(RC999.getCode(), RC999.getMessage());
        }
    }

    @RequestMapping(value = "/addBalance", method = RequestMethod.POST)
    @ResponseBody
    public ResultData addBalance(@RequestBody Map<String, Object> reqBody, HttpServletRequest request, Authentication auth) throws Exception {
        SecurityUser sUser = (SecurityUser) auth.getPrincipal();
        String createAccount = sUser.getUsername();
        String ttc = sUser.getTTC();
        Integer money = (Integer) reqBody.getOrDefault("money", 0);
        String opTTC = (String) reqBody.get("ttc");
        try {
            if(createAccount.equals("admin") && ttc.equals("admin")) {
                superAdminService.addBalance(opTTC, money);
                return ResultData.success();
            }else{
                return ResultData.fail(RC999.getCode(), RC999.getMessage());
            }
        }catch (Exception e){
            return ResultData.fail(RC999.getCode(), RC999.getMessage());
        }
    }

    @RequestMapping(value = "/addGift", method = RequestMethod.POST)
    @ResponseBody
    public ResultData addGift(@RequestBody Map<String, Object> reqBody, HttpServletRequest request, Authentication auth) throws Exception {
        SecurityUser sUser = (SecurityUser) auth.getPrincipal();
        String createAccount = sUser.getUsername();
        String ttc = sUser.getTTC();
        Integer money = (Integer) reqBody.getOrDefault("money", 0);
        String opTTC = (String) reqBody.get("ttc");
        try {
            if(createAccount.equals("admin") && ttc.equals("admin")) {
                superAdminService.addGift(opTTC, money);
                return ResultData.success();
            }else{
                return ResultData.fail(RC999.getCode(), RC999.getMessage());
            }
        }catch (Exception e){
            return ResultData.fail(RC999.getCode(), RC999.getMessage());
        }
    }
}
